package HeSuanServer.service.Impl;

import HeSuanServer.mapper.UserCliMapper;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeException;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class QrCodeService {
    @Value("${img.logo}")
    private String img;
//    @Autowired
//    private QrConfig config;
    @Autowired
    private UserCliMapper userCliMapper;
    QrConfig getQrConfig(int foreColor,int backColor){
//        InputStream inputStream;
        File file=new File("C:/Users/Administrator/Desktop/lh/HeSuan/src/main/resources/img/"+img);
//        try {
////            inputStream=new FileInputStream(file);
//        } catch (FileNotFoundException e) {
//            throw new RuntimeException(e);
//        }
        //初始宽度和高度
        QrConfig qrConfig=new QrConfig(250,250);
        //设置边距，即二维码和边框的距离
        qrConfig.setMargin(2);
        qrConfig.setImg(file);
        qrConfig.setForeColor(foreColor);
        qrConfig.setBackColor(backColor);
        return qrConfig;

    }

    //生成到文件
    public void createCodeToFile(String content, String filePath,int foreColor,int backColor) {
        try {
            QrCodeUtil.generate(content,getQrConfig(foreColor,backColor), FileUtil.file(filePath));
        } catch (QrCodeException e) {
            e.printStackTrace();
        }
    }
    //生成到流
    public void createCodeToStream(String msg, QrConfig qrConfig, HttpServletResponse response) {
        try {
            QrCodeUtil.generate(msg,qrConfig, "png", response.getOutputStream());
        } catch (QrCodeException | IOException e) {
            e.printStackTrace();
        }
    }

    public void getCodeByStatus(String id, HttpServletResponse servletResponse) {
        Map<String,Object> map=new HashMap<>();
        map.put("id",id);
        DateTime dateTime=new DateTime(new Date().getTime()-7*24*60*60*1000);
        map.put("start",dateTime);
        map.put("end",new DateTime());
        QrConfig qrConfig;
//      查询近七天的核酸记录返回查询结果一条
        String statues=userCliMapper.getStatusById(map);
        if("1".equals(statues)){
            qrConfig=getQrConfig(new Color(32, 154, 57).getRGB(),Color.WHITE.getRGB());
        }else if("2".equals(statues)){
            qrConfig=getQrConfig(Color.red.getRGB(),Color.WHITE.getRGB());
        }else if("3".equals(statues)){
            qrConfig=getQrConfig(Color.orange.getRGB(),Color.WHITE.getRGB());
        }else {
            qrConfig=getQrConfig(Color.BLACK.getRGB(),Color.WHITE.getRGB());
        }
        createCodeToStream(id,qrConfig,servletResponse);
    }
}
